//教材章节控制类。
Ext.define("ES.controller.SectionsController",{
    extend:"Ext.app.Controller",
    models:["TeachingSectionsModel","CatalogModel"],
    stores:["TeachingSectionsStore","CatalogTreeStore","NotSelfSectionsStore","CatalogStore"],
    views:["sections.SectionsList","sections.SectionsEdit"],
    catalogId:"",
    init:function(){
        this.control({
            "sectionsTabList>treepanel":{
                itemclick:function(view,record,item,index,e,opts){
                   this.catalogId = record.data.ID;
                   var btn = Ext.getCmp("sectionsTabListbtnQuery");
                   if(btn){
                       btn.fireEvent("click", btn);
                   }
                }
            },
            "sectionsTabList>gridpanel":{
                afterrender:function(cmp){
                     var btn = cmp.down("toolbar button[action=btnQuery]");
                     this.onSearch(btn);
                },
                itemdblclick:this.onEdit
            },
            "sectionsTabList>gridpanel button[action=btnQuery]":{
                click:this.onSearch
            },
            "sectionsTabList>gridpanel button[action=btnAdd]":{
                click:this.onAdd
            },
            "sectionsTabList>gridpanel button[action=btnDelete]":{
                click:this.onDelete
            },
            "sectionsEdit combo[name=catalogID]":{
                beforerender:function(combo){
                    var store = combo.getStore();
                    if(store){
                        store.load({
                            callback: function(records, operation, success){
                                if(success && this.catalogId){
                                    combo.setValue(this.catalogId);
                                }
                            }
                        });
                    }
                }
            },
            "sectionsEdit button[action=btnSave]":{
                click:this.onSave
            },
            "sectionsEdit button[action=btnCancel]":{
               click:function(btn){
                    var win = btn.up("sectionsEdit");
                    if(win){
                        win.close();
                    }
                } 
            }
        });
    },
    onSearch:function(btn){
        var grid = Ext.getCmp("sectionsTabListgridpanel"); 
        if(grid){
            var st = grid.getStore();
            var toolbar = btn.up("toolbar"); 
            if(toolbar){
                st.loadPage(1,{
                    params:{
                        sectionName:toolbar.getComponent(1).getValue(),
                        catalogID:this.catalogId
                    }
                });
            }
        }
    },
    onAdd:function(){
        var win = Ext.widget("sectionsEdit");
        if(win){
            win.show();
            var parentStore = this.getNotSelfSectionsStoreStore();
            parentStore.load({
                callback: function(records, operation, success){
                    if(success){
                        //
                    }
                }
            });
        }
    },
    onEdit:function(view, record, item,index,e,opt){
        var win = Ext.widget("sectionsEdit");
        if(win){
            win.show();
            var parentStore = this.getNotSelfSectionsStoreStore(); 
            parentStore.load({
                callback: function(records, operation, success){
                    if(success){
                        var frm = win.down("form");
                        frm.loadRecord(record);   
                    }
                }
            });
        }
    },
    onSave:function(btn){
        var win = btn.up("sectionsEdit");
        var form = btn.up("form").getForm();
        if(form.isValid()){
            var store = this.getTeachingSectionsStoreStore();
            var record = form.getRecord();
            var values = form.getValues();
            if(record){
                record.set(values);
            }else{
                record = this.getTeachingSectionsModelModel().create();
                record.set(values);
                store.insert(0,record);
            } 
            store.sync(); 
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                    Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                    var el = grid.getEl()
                    el.mask("正在删除数据，请稍后...");
                    var st = grid.getStore();
                    Ext.Array.each(data, function(record){
                        st.remove(record);
                    })
                    st.sync();
                    el.unmask();
                }
            }
        });
    }
})